package com.portal.innovation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.portal.innovation.common.Constant;
import com.portal.innovation.utils.PageUtils;
import com.portal.innovation.dao.NewsDao;
import com.portal.innovation.pojo.News;
import com.portal.innovation.service.NewsService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@Service("newsService")
public class NewsServiceImpl extends ServiceImpl<NewsDao, News> implements NewsService {


    @Override
    public PageUtils queryPage(HashMap params) {
        Integer curPage = 1;
        Integer limit = 10;

        if (params.get(Constant.PAGE) != null) {
            curPage = Integer.parseInt(params.get(Constant.PAGE).toString());
        }
        if (params.get(Constant.LIMIT) != null) {
            limit = Integer.parseInt(params.get(Constant.LIMIT).toString());
        }
        //分页对象
        Page<News> page = new Page<>(curPage, limit);

        QueryWrapper<News> condition = new QueryWrapper<>();
        if (params.containsKey("title")) {
            String title = params.get("title").toString();
            condition.like(!StringUtils.isBlank(title), "title", title);
        }
        if (params.containsKey("order")) {
            String order = params.get("order").toString();
            condition.orderBy(!StringUtils.isBlank(order), order.equals("asc"), "publish_time");
        }
        if (params.containsKey("daterange")) {
            List<String> dateRange = (List<String>) params.get("daterange");
            if (dateRange != null && CollectionUtils.isNotEmpty(dateRange)) {
                condition.between("publish_time",
                        LocalDate.parse(dateRange.get(0)),
                        LocalDate.parse(dateRange.get(1)));
            }
        }

        PageUtils pageUtils = new PageUtils(this.page(page, condition));
        return pageUtils;
    }

    @Override
    public void saveNews(News news) {
        news.setPublishTime(new Date());
        news.setVisitNum(0);
        news.setStatus(true);
        //TODO
        news.setAdmin("");
        save(news);
    }

    @Override
    public PageUtils conditionSearch(HashMap params) {
        Integer curPage = 1;
        Integer limit = 10;

        if (params.get(Constant.PAGE) != null) {
            curPage = Integer.parseInt(params.get(Constant.PAGE).toString());
        }
        if (params.get(Constant.LIMIT) != null) {
            limit = Integer.parseInt(params.get(Constant.LIMIT).toString());
        }
        //分页对象
        Page<News> page = new Page<>(curPage, limit);

        QueryWrapper<News> condition = new QueryWrapper<>();
        condition.eq("status", true);
        if (params.containsKey("title")) {
            String title = params.get("title").toString();
            condition.like(!StringUtils.isBlank(title), "title", title);
        }
        if (params.containsKey("order")) {
            String order = params.get("order").toString();
            condition.orderBy(!StringUtils.isBlank(order), order.equals("asc"), "publish_time");
        }
        PageUtils pageUtils = new PageUtils(this.page(page, condition));
        return pageUtils;
    }

    @Override
    public News getDetailById(Integer id) {
        News news = getById(id);
        news.setVisitNum(news.getVisitNum() + 1);
        updateById(news);
        return news;
    }
}